var routerApp = angular.module('BLS');
routerApp.run(function($rootScope,$state,$templateCache) {
/*    var stateChangeSuccess = $rootScope.$on('$stateChangeSuccess', stateChangeSuccess);  
      function stateChangeSuccess($rootScope) {  
       $templateCache.removeAll();    
    }*/ 
    $rootScope.$state = $state; 
});

routerApp .config(function($stateProvider, $urlRouterProvider,$httpProvider,$sceProvider,$ionicConfigProvider) {
    $ionicConfigProvider.tabs.position('bottom');

    // Use x-www-form-urlencoded Content-Type
    if( !$httpProvider.defaults.headers.get ) 
    {
        $httpProvider.defaults.headers.get = {};
    };   
    $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
    $httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    /**
     * The workhorse; converts an object to x-www-form-urlencoded serialization.
     * @param {Object} obj
     * @return {String}
     */
    var param = function(obj) {
        var query = '', name, value, fullSubName, subName, subValue, innerObj, i;

        for(name in obj) {
            value = obj[name];

            if(value instanceof Array) {
                for(i=0; i<value.length; ++i) {
                    subValue = value[i];
                    fullSubName = name + '[' + i + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if(value instanceof Object) {
                for(subName in value) {
                    subValue = value[subName];
                    fullSubName = name + '[' + subName + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if(value !== undefined && value !== null)
                query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
        }

        return query.length ? query.substr(0, query.length - 1) : query;
    };
    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function(data) {
        return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];
    $stateProvider
        .state('testList', {
            url: '/testList',
            templateUrl: 'tpl/testList.html',
            cache:'false',
            controller: 'TestListCtrl'
        })
        .state('main', {
            url: '/main',
            cache:'false',
            templateUrl: 'tpl/main.html',
            controller: 'BLSCtrl'
        })
        .state('main.bls',{
            url:'/bls',
            cache:'false',
            views:{
                'bls':{
                    templateUrl: 'tpl/bls/bls.html',
                }
            }
        })
        .state('main.myself',{
            url:'/myself',
            cache:'false',
            views:{
                'myself':{
                    templateUrl: 'tpl/myself/myself.html',
                }
            }
        })
        .state('zhangjieList',{
            url:'/zhangjieList',
            templateUrl: 'tpl/bls/zhangjie/listSelect.html',
            cache:'false',
            controller: 'ListSelectCtrl'
        })
        .state('zhangjieContent',{
            url:'/zhangjieContent',
            cache:'false',
            templateUrl: 'tpl/bls/zhangjie/content.html',
            controller: 'ContentCtrl'
        })
        .state('dianti1',{
            url:'/dianti1',
            cache:'false',
            templateUrl: 'tpl/bls/dianti/dianti1.html',
            controller: 'Dianti1Ctrl'
        })
        .state('diantixieyi',{
            url:'/diantixieyi',
            cache:'false',
            templateUrl: 'tpl/bls/dianti/diantixieyi.html',
            //controller: 'Dianti1Ctrl'
        })
        .state('diantiselect',{
            url:'/diantiselect',
            cache:'false',
            templateUrl: 'tpl/bls/dianti/diantiselect.html',
            //controller: 'Dianti1Ctrl'
        })
        .state('diantibuy',{
            url:'/diantibuy',
            cache:'false',
            templateUrl: 'tpl/bls/dianti/diantibuy.html',
            //controller: 'Dianti1Ctrl'
        })
        .state('diantiyigoumai',{
            url:'/diantiyigoumai',
            cache:'false',
            templateUrl: 'tpl/bls/dianti/diantiyigoumai.html',
            //controller: 'Dianti1Ctrl'
        })
        .state('tongguaninit',{
            url:'/tongguaninit',
            cache:'false',
            templateUrl: 'tpl/bls/tongguan/tongguaninit.html',
            //controller: 'Dianti1Ctrl'
        })
        .state('tongguandetail',{
            url:'/tongguandetail',
            cache:'false',
            templateUrl: 'tpl/bls/tongguan/tongguandetail.html',
            //controller: 'Dianti1Ctrl'
        })
        .state('tongguanbuy',{
            url:'/tongguanbuy',
            cache:'false',
            templateUrl: 'tpl/bls/tongguan/tongguanbuy.html',
            //controller: 'Dianti1Ctrl'
        })
        .state('wenkuinit',{
            url:'/wenkuinit',
            cache:'false',
            templateUrl: 'tpl/bls/wenku/wenkuinit.html',
            //controller: 'Dianti1Ctrl'
        })
        .state('wenkuselect',{
            url:'/wenkuselect',
            cache:'false',
            templateUrl: 'tpl/bls/wenku/wenkuselect.html',
            //controller: 'Dianti1Ctrl'
        })
        .state('wenkudetail',{
            url:'/wenkudetail',
            cache:'false',
            templateUrl: 'tpl/bls/wenku/wenkudetail.html',
            //controller: 'Dianti1Ctrl'
        })
        .state('zhinaninit',{
            url:'/zhinaninit',
            cache:'false',
            templateUrl: 'tpl/bls/zhinan/zhinaninit.html',
            //controller: 'Dianti1Ctrl'
        })
        .state('zhinandetail',{
            url:'/zhinandetail',
            cache:'false',
            templateUrl: 'tpl/bls/zhinan/zhinandetail.html',
            //controller: 'Dianti1Ctrl'
        })

    ;$urlRouterProvider.otherwise('/testList');

});











